

#include <WProgram.h>
//#include <HardwareSerial.cpp>

#include <ME218_8x8_charset_PGM.c>

#include <ME218_I2C_top.c>

#include <ME218_I2C.c>

#include <ME218_CLUT_PGM.c>

#include <ME218_CLUT.c>

#include <ME218_Sprite_PGM.c>

#include <ME218_Sprites.c>

#include <ME218_ISR.c>
#include <ME218_work.c>


//void ME218_Init(void);
//void TWI_begin(uint8_t);
void ClearMeggy(void);

 
void init(){
	
	ME218_Init();
	TWI_begin(0x50);
	
	
}

unsigned char	dracula;


void setup() {
//	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
//	Serial.println("------------------");
//	Serial.println("   hello world");
//	Serial.println("------------------");
	
	
	ClearMeggy();

 //	start_LED_sprite(0);
	write_out_LED_value(0);
	
//	draw_ASCII_LED_char('W');

	scroll_LED_TEST();

};


void loop () {
	
	int lap_counter;
	int i,k,j,n,m;
	
	
	int temp_TT_cnt, temp_TT_vs,temp_TT_hs, temp_TT_sx, temp_TT_sy, temp_TT_rt;  
	int temp_SS_cnt, temp_SS_vs,temp_SS_hs, temp_SS_sx, temp_SS_sy, temp_SS_rt;  
	unsigned long temp_SS_dly;
	int temp_ls_index;
	prog_uchar *TT_dd;
	
	prog_uchar *temp_SS_raw;
	prog_uchar *work_raw;
	
	unsigned char temp;
	int temp2;
	int xx,yy;
	unsigned long numerator;
	unsigned long denominator;
	
	SINGLE_SPRITE_ARRAY*		TT_SINGLE_SPRITE;
	ALL_GRAPHIC_SPRITE_ARRAY*	TT_ALL_SPRITE;

	
	
//	temp = pgm_read_word_near(&DOG);
//	while (temp==7) {};
	
	// TIP_TOP_T0TAL = 5
	
	//  initializeSPRITEs();
	

	
	
//	write_out_LED_value(0x1);	
//	start_LED_sprite(0);
	
//	write_out_LED_value(0xF0);
   
	//   for(k=0;k<3;k++) {
//	while (1) {
		
//	ddelay(50000);
	dracula++;
//		draw_char(dracula);
//			draw_string2("What is going on? ");
//	draw_2_chars('A','B');
//	ddelay(50000);
		
        
		lap_counter++;
		//  rock_roll_i2c();
		
		//read_MO_keypad();
		// ClearOffScreenBuffer();
		//                          Meg.OffScreenGameSlate[0][0]=2;
		
		// let's try some i2c
		
		
		
		for(k=0;k<TIP_TOP_TOTAL;k++) {
			
			
			if(SpriteTable[k].ls_delay==0) {
				TT_ALL_SPRITE=(ALL_GRAPHIC_SPRITE_ARRAY *)SUPER_MAGIC_SPRITE_VARIABLE;
				
				
				TT_SINGLE_SPRITE=(SINGLE_SPRITE_ARRAY *)pgm_read_word_near(&TT_ALL_SPRITE[k].init_TIPTOP_SPRITE_frames_array);
				temp_ls_index=SpriteTable[k].ls_index;
				TT_SINGLE_SPRITE+=temp_ls_index;
				
				temp_SS_raw=(prog_uchar *)pgm_read_word_near(&TT_SINGLE_SPRITE->GRAPHIC_SPRITE_raw_data); 
				
				
                temp_SS_dly=(prog_uint16_t)pgm_read_word_near(&TT_SINGLE_SPRITE->starting_SPRITE_delay);
				
				numerator=SpriteTable[k].ls_speed_numerator;
				denominator=SpriteTable[k].ls_speed_denominator;
				SpriteTable[k].ls_delay=(numerator*temp_SS_dly)/denominator; // reset counter
				
				
				
				//  temp_ls_index=Meg.SpriteTable[k].ls_index;
				temp_SS_cnt=SpriteTable[k].ls_total_cnt;
				
				//   temp_SS_raw=Meg.SpriteTable[k].ls_raw_data;
				
				temp_TT_hs=SpriteTable[k].ls_horizontal_size;
				temp_TT_vs=SpriteTable[k].ls_vertical_size;
				
				temp_TT_sx=SpriteTable[k].ls_current_x;
				temp_TT_sy=SpriteTable[k].ls_current_y;
				
				
				for(m=0;m<temp_TT_hs;m++) {
					
					for (i=0;i<temp_TT_vs;i++) {
						work_raw= temp_SS_raw + m*temp_TT_hs + i;
						
						temp=pgm_read_byte_near(work_raw);
						
						xx=temp_TT_sx+i;    // x current location
						yy=temp_TT_sy+m;    // y current location
						
						
						
						// the pixel must not be clear or invisible
						//  if(temp!=0 && Meg.SpriteTable[k].ls_visible)     //  do not draw a clear pixel
						if(xx>=0 && xx<8 && yy>=0 && yy<8 ) {   // is the pixel on screen
							if(SpriteTable[k].ls_visible)   //  do not draw a clear pixel                                     
								OffScreenGameSlate[yy][xx]=temp;
						}
					}
				}
				
				
				if(SpriteTable[k].ls_rotation) {              // clockwise
					SpriteTable[k].ls_index++;
					
					if(SpriteTable[k].ls_index>=temp_SS_cnt)
						SpriteTable[k].ls_index=0;
					
				}
				else {
					
					
					SpriteTable[k].ls_index--;
					
					if(SpriteTable[k].ls_index<0) {
						
						SpriteTable[k].ls_index=temp_SS_cnt-1;
						
					}
				} // rotation
			} // if delay
			SpriteTable[k].ls_delay--;      //count down
		} // for
//	} // while
 
} // loop







int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

